package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.DetailLabelUpdateDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateBatchDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateBatchVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.label.pojo.dto.AllLabelShowVO;
import com.xbongbong.pro.label.pojo.dto.LabelListDTO;
import com.xbongbong.pro.label.pojo.dto.LabelShowVO;
import com.xbongbong.saas.service.LabelService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author guohui.zhang
 * @Title: 移动端标签
 * @date 2020/11/1910:25
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/label/set")
public class MobileLabelController {
    @Resource
    private LabelService labelService;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    /**
     * 标签列表
     * @param labelListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/simpleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String simpleList(@RequestBody @Valid LabelListDTO labelListDTO, BindingResult br) throws Exception {
        XbbResponse<LabelShowVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelShowVO labelShowVO = labelService.getAllLabelList(labelListDTO);
                response = new XbbResponse<>(labelShowVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 个人以及企业标签
     * @param labelListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/labelList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String tabelList(@RequestBody @Valid LabelListDTO labelListDTO, BindingResult br) throws Exception {
        XbbResponse<LabelShowVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AllLabelShowVO allLabelShowVO = labelService.getPersonLabelList(labelListDTO);
                response = new XbbResponse<>(allLabelShowVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 移动端用来详情页更新个人标签和企业标签
     * @param detailLabelUpdateDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDetailLabel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateDetailLabel(@RequestBody @Valid DetailLabelUpdateDTO detailLabelUpdateDTO, BindingResult br) {
        XbbResponse<FormDataUpdateBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(detailLabelUpdateDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(detailLabelUpdateDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                FormDataUpdateBatchDTO formDataUpdateBatchDTO = detailLabelUpdateDTO.getFormDataUpdateBatchDTO();
                TransmittableThreadLocalUtil.setValue(formDataUpdateBatchDTO.getSaasMark(), formDataUpdateBatchDTO.getBusinessType(), formDataUpdateBatchDTO.getMenuId(), formDataUpdateBatchDTO.getDistributorMark());
                FormDataUpdateBatchVO formDataUpdateBatchVO = labelService.updateDetailLabel(detailLabelUpdateDTO);
                response = new XbbResponse<>(formDataUpdateBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
